<template>
  <view class="content">
    <view class="index">
      <!-- 首页轮播 -->
      <swiper-image :resdata="swiper"></swiper-image>
      <view class="nws-lamu">
        <uni-section title="文字滚动" subTitle type="line" speed="6000" v-if="announcement.show_notify">
          <uni-notice-bar
            show-icon
            scrollable
            :text="announcement.title"
            background-color="#f00"
            color="#fff"
            style="border-radius: 10upx;"
            @click="tongz('通知')"
          />
        </uni-section>
        <view class="diancna-nav">
          <view
            class="diancna-nav-a"
            v-for="(item,index) in rownav"
            :key="index"
            @click="navck(item.path)"
          >
            <image :src="item.src" mode="aspectFill" />
            <text class="uni-ellipsis">{{item.txt}}</text>
          </view>
        </view>
      </view>
      <view class="idx-box">
        <view class="idx-img" v-for="(item,index) in adimg2" :key="index">
          <image :src="item.picture" mode="widthFix" />
        </view>
      </view>
      <view class="idx-list">
        <view class="idx-list-bt">
          <image src="../../static/col-title.png" mode="heightFix" />
		  <button type="default" class="idxmoren-btn" @click="sfmoren()">更多</button>
        </view>
        <view class="idx-list-box">
          <view
            class="idx-list-a"
            @click="details(item.id)"
            v-for="(item,index) in idxlist"
            :key="index"
          >
            <view class="idx-list-aimg">
              <image :src="item.cover" mode />
            </view>
            <view class="idx-list-txt">
              <text class="idx-list-txt-h4">{{item.title}}</text>
              <view class="idx-list-txt-wb">
                <view>
                  {{item.discount_price}}
                  <text>{{item.market_price}}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view class="idx-list">
        <view class="idx-list-bt">
          <image src="../../static/col-title2.png" mode="heightFix" />
		  <button type="default" class="idxmoren-btn" @click="dzmoren()">更多</button>
        </view>
        <view class="idx-list-box">
          <view
            class="idx-list-a"
            @click="details(item.id)"
            v-for="(item,index) in idxlist2"
            :key="index"
          >
            <view class="idx-list-aimg">
              <image :src="item.cover" mode />
            </view>
            <view class="idx-list-txt">
              <text class="idx-list-txt-h4">{{item.title}}</text>
              <view class="idx-list-txt-wb">
                <view>
                  {{item.discount_price}}
                  <text>{{item.market_price}}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <dacizinavtag :resdata="swiper"></dacizinavtag>
  </view>
</template>

<script>
import swiperImage from "@/components/index/swiper-image.vue";
import dacizinavtag from "@/components/dacizi-navtag/dacizi-navtag.vue";
export default {
  components: {
    dacizinavtag,
    swiperImage
  },

  data() {
    return {
      item_index: 0, // 单页面id
      daya: "123",
      referrer:'',
      swiper: [],
      rownav: [
        {
          src: "../../static/col-img3.png",
          txt: "个性点餐",
          path: "/pages/my/custom-list"
        },
        {
          src: "../../static/col-img4.png",
          txt: "私房菜",
          path: "/pages/my/private"
        }
      ],
      adimg2: [],
      adimg: [],
      announcement: "",
      idxlist: [],
      idxlist2: [],
      share: {
        title: "花儿私厨",
        path: "/pages/index/index",
        imageUrl: "",
        desc: "",
        content: ""
      }
    };
  },
  onLoad(data) {
    console.log(data.scene);
		if(data.scene){
			this.referrer = data.scene
		}
    var than = this;
    uni.getLocation({
      type: "wgs84",
      isHighAccuracy: true,
      success(res) {
        const latitude = res.latitude;
        const longitude = res.longitude;
        const speed = res.speed;
        const accuracy = res.accuracy;
        console.log(latitude, longitude, accuracy);

        uni.setStorageSync("latitude", latitude); //纬度
        uni.setStorageSync("longitude", longitude); //经度
        uni.request({
          url: getApp().globalData.http + "/district/cityLocation",
          data: {
            lng: longitude,
            lat: latitude
          },
          method: "post",
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          success: res => {
            uni.setStorageSync("chengshi", res.data.data);
          }
        });
      }
    });
    this.logo();
  },
  onShow() {
    wx.hideHomeButton();
  },
  methods: {
    logo() {
      var than = this;
      uni.login({
        success(res) {
          if (res.code) {
            //发起网络请求
            uni.request({
              url: getApp().globalData.http + "/user/auth",
              data: {
                code: res.code,
                referrer:than.referrer
              },
              method: "post",
              header: {
                "content-type": "application/x-www-form-urlencoded"
              },
              success: res => {
                uni.setStorageSync("token", res.data.data.token);
                than.getInfo();
                than.getBanner1();
                than.getBanner2();
                than.jyfw();
                than.getList1();
                than.getList2();
                than.gonggao();
              }
            });
          } else {
            console.log("登录失败！" + res.errMsg);
          }
        }
      });
    },
    getInfo() {
      var than = this
      uni.request({
        url: getApp().globalData.http + "/user/info",
        data: "",
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          uni.setStorageSync("user", res.data.data);
        }
      });
    },
    getBanner1() {
      uni.request({
        url: getApp().globalData.http + "/banner/index",
        data: {
          position: 1
        },
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          res.data.data.forEach(e => {
            this.swiper.push({
              src: e.picture
            });
          });
        }
      });
    },
    getBanner2() {
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/banner/index",
        data: {
          position: 2
        },
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            than.adimg2 = res.data.data;
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    gonggao() {
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/config/notify",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            than.announcement = res.data.data;
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    jyfw() {
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/config/business",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            uni.setStorageSync("jyfw", res.data.data);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    getList1() {
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/recommend/index",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            than.idxlist = res.data.data;
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    getList2() {
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/recommend/normal",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            than.idxlist2 = res.data.data;
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    details(id) {
      wx.navigateTo({
        url: "/pages/my/private-info?id=" + id
      });
    },
    tongz(num) {
      wx.navigateTo({
        url: "/pages/my/inform?id=" + num
      });
    },
    navck(url) {
      wx.navigateTo({
        url: url
      });
    },
    navck2(url) {
      wx.navigateTo({
        url: url
      });
    },
    //定制更多
    dzmoren() {
      wx.navigateTo({
        url: "../my/private"
      });
    },
    //私房更多
    sfmoren() {
      wx.navigateTo({
        url: "../my/private"
      });
    },
    onShareAppMessage(res) {
      return {
        title: this.share.title,
        path: this.share.path,
        imageUrl: this.share.imageUrl,
        desc: this.share.desc,
        content: this.share.content,
        success(res) {
          uni.showToast({
            title: "分享成功"
          });
        },
        fail(res) {
          uni.showToast({
            title: "分享失败",
            icon: "none"
          });
        }
      };
    },
    onShareTimeline(res) {
      return {
        title: this.share.title,
        path: this.share.path,
        imageUrl: this.share.imageUrl,
        desc: this.share.desc,
        content: this.share.content,
        success(res) {
          uni.showToast({
            title: "分享成功"
          });
        },
        fail(res) {
          uni.showToast({
            title: "分享失败",
            icon: "none"
          });
        }
      };
    }
  }
};
</script>